﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using CAUNetLogin.Util.Security;
using CAUNetLogin.Entity;
using CAUNetLogin.Util.SystemLib;
using System.Windows.Forms;

namespace CAUNetLogin.Util.Entity
{
    /// <summary>
    /// 用户配置信息管理
    /// </summary>
    public static class ConfigManager
    {
        public static string configFilePath = @"C:\Users\" + Environment.UserName + @"\AppData\Local\config.ini";
        public static bool isError = false;
        public static string lastErrorMessage = "";

        public static bool savePassword = false;//保存密码
        public static bool startWithWindows = false;//开机启动
        public static bool autoConnect = false;//自动连接
        public static bool exitAfterConnect = false;//连接后退出程序
        public static bool runAsAdministrator = false;//管理员权限运行
        public static string autoRunKeyName = "CAUNetLogin";//注册表中开启启动键值
        /// <summary>
        /// 保存用户密码
        /// </summary>
        /// <returns></returns>
        public static bool SaveAccount(Account user)
        {
            try
            {
                FileStream fs = new FileStream(ConfigManager.configFilePath, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write("这是CAU网关登录器保存的密码信息,请不要删除\n" + SimpleSecurity.Encode(user.User) + "\n" + SimpleSecurity.Encode(user.Password) + "\n");
                sw.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                lastErrorMessage = ex.Message;
                isError = true;
                return false;
            }
            isError = false;
            return true;
        }
        /// <summary>
        /// 获得用户信息
        /// </summary>
        /// <returns></returns>
        public static bool GetAccount(ref Account user)
        {
            try
            {
                FileStream fs = new FileStream(ConfigManager.configFilePath, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                sr.ReadLine();
                user.User = SimpleSecurity.Decode(sr.ReadLine());
                user.Password = SimpleSecurity.Decode(sr.ReadLine());
                sr.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                lastErrorMessage = ex.Message;
                isError = true;
                return false;
            }
            isError = false;
            return true;
        }


        /// <summary>
        /// 获取是否开机启动
        /// </summary>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public static bool CheckAutoStart(string keyName)
        {
            bool result = Registery.LMCheckRegistery(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", keyName);
            runAsAdministrator = !Registery.isException;
            return result;
        }

        /// <summary>
        /// 更新选择项
        /// </summary>
        /// <param name="savePassword">保存密码</param>
        /// <param name="startWithWindows">开机启动</param>
        /// <param name="autoConnect">自动连接</param>
        /// <param name="exitAfterConnect">自动连接后退出</param>
        /// <param name="result">返回的信息</param>
        /// <returns>是否更新成功</returns>
        public static bool UpdateChoice(bool savePassword, bool startWithWindows, bool autoConnect, bool exitAfterConnect, ref string result,Account user=null)
        {
            bool flag = true;
            if (ConfigManager.savePassword != savePassword)
            {
                ConfigManager.savePassword = savePassword;
                result = (savePassword == true ? "【保存密码】设置【成功】\n" : "【取消保存密码】设置【成功】\n");
            }
            
            if (ConfigManager.startWithWindows != startWithWindows)
            {
                if (ConfigManager.runAsAdministrator)//以管理员权限运行
                {
                    if (startWithWindows == true)
                    {
                        if (Registery.LMSetRegistery(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", autoRunKeyName, Application.ExecutablePath))
                        {
                            result = "【开机启动项】添加【成功】\n";
                            ConfigManager.startWithWindows = startWithWindows;
                        }
                        else
                        {
                            result = "【开机启动项】添加【失败】\n";
                            flag = false;
                        }
                    }
                    else
                    {
                        if (Registery.LMDeleteRegistery(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", autoRunKeyName))
                        {
                            result = "【开机启动项】删除【成功】\n";
                            ConfigManager.startWithWindows = startWithWindows;
                        }
                        else
                        {
                            result = "【开机启动项】删除【失败】\n";
                            flag = false;
                        }
                    }
                }
            }
            if (ConfigManager.autoConnect != autoConnect)
            {
                string keyValue = (autoConnect == true ? "1" : "0");
                if (Registery.CUSetRegistery(@"Software\CAUNetLogin", "AutoConnect", keyValue))
                {
                    result = (autoConnect == true ? "【自动连接网络】设置【成功】\n" : "【取消自动连接网络】设置【成功】\n");
                    ConfigManager.autoConnect = autoConnect;
                }
                else
                {
                    result = (autoConnect == true ? "【自动连接网络】设置【失败】\n" : "【取消自动连接网络】设置【失败】\n");
                    flag = false;
                }
            }
            if (ConfigManager.exitAfterConnect != exitAfterConnect)
            {
                string keyValue = (exitAfterConnect == true ? "1" : "0");
                if (Registery.CUSetRegistery(@"Software\CAUNetLogin", "ExitAfterConnect", keyValue))
                {
                    result = (exitAfterConnect == true ? "【连接网络后退出】设置【成功】\n" : "【取消连接网络退出程序】设置【成功】\n");
                    ConfigManager.exitAfterConnect = exitAfterConnect;
                }
                else
                {
                    result = (exitAfterConnect == true ? "【连接网络后退出】设置【失败】\n" : "【取消连接网络退出程序】设置【失败】\n");
                    flag = false;
                }
            }
            return flag;
        }
    }
}
